package com.zrrd.learn.chapter09.collectlianxi;

import java.util.*;

/**
 * 集合一般07
 *
 * @author wrn
 * @version 1.0
 * @date 2023/8/18 15:45
 */

public class collect07 {

    public static void question01(){

        List<Student07> list = new ArrayList<>();

        list.add(new Student07("Tom", 18, 100, "class05"));
        list.add(new Student07("Jerry", 22, 70, "class04"));
        list.add(new Student07("Owen", 25, 90, "class05"));
        list.add(new Student07("Jim", 30,80 , "class05"));
        list.add(new Student07("Steve", 28, 66, "class06"));
        list.add(new Student07("Kevin", 24, 100, "class04"));

        Iterator<Student07> it = list.iterator();
        int sum = 0;
        int age = 0;
        while (it.hasNext()) {
            sum += it.next().getAge();
        }
        age=sum/ list.size();

        System.out.println("所有学生的平均年龄:"+age);

    }

    public static void question02(){
        ArrayList<String>list=new ArrayList<String>();
        Collections.addAll(list, "科迪瓦特","阿根廷","澳大利亚","塞尔维亚","荷兰",
                "尼日利亚","日本","美国","中国","新西兰","巴西","比利时","韩国","喀麦隆","洪都拉斯","意大利");
        int g1=0,g2=0,g3=0,g4=0;
        Random r=new Random();
        Map<String,Integer>group=new HashMap<String, Integer>();
        int j;
        for(int i=0;i<list.size();i++)
        {
            j=r.nextInt(4)+1;
            if(j==1&&g1<4)
            {
                group.put(list.get(i),j);
                g1++;
            }
            else if(j==2&&g2<4)
            {
                group.put(list.get(i),j);
                g2++;
            }
            else if(j==3&&g3<4)
            {
                group.put(list.get(i),j);
                g3++;
            }
            else if(j==4&&g4<4)
            {
                group.put(list.get(i),j);
                g4++;
            }
            else {
                i--;
            }
        }
    /*for(int i=1;i<=4;i++)
    {
        System.out.print("第"+i+"组：");
        for(Entry<String, Integer>entry:group.entrySet()) {
            if(entry.getValue().equals(i))
            {
                System.out.print(entry.getKey()+" ");
            }
        }
        System.out.println(" ");
    }
    */
        for(int i=1;i<=4;i++)
        {
            System.out.print("第"+i+"组：");
            Set kSet=group.keySet();
            Iterator it=kSet.iterator();
            while(it.hasNext())
            {
                Object key=(Object)it.next();
                Object value=group.get(key);
                if(value.equals(i))
                {
                    System.out.print(key+" ");
                }
            }
            System.out.println(" ");
        }

    }


    public static void main(String[] args) {

       question02();

    }

}
